<template>
  <div class="employee-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>员工管理</span>
          <el-button type="primary" @click="addEmployee">新增员工</el-button>
        </div>
      </template>

      <el-table :data="employees" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="role" label="角色">
          <template #default="{ row }">
            {{ roleMap[row.role] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ statusMap[row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="editEmployee(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteEmployee(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.size"
        :total="total"
        @current-change="getList"
        layout="prev, pager, next"
        style="margin-top: 20px;"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑员工' : '新增员工'" width="50%">
      <el-form ref="formRef" :model="formData" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名">
              <el-input v-model="formData.username"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名">
              <el-input v-model="formData.name"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input v-model="formData.phone"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="formData.sex" placeholder="请选择">
                <el-option label="男" value="男"/>
                <el-option label="女" value="女"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号">
              <el-input v-model="formData.id_number"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="formData.role" placeholder="请选择">
                <el-option label="管理员" :value="0"/>
                <el-option label="仓库主管" :value="1"/>
                <el-option label="普通员工" :value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!isEdit">
            <el-form-item label="密码">
              <el-input v-model="formData.password" type="password" show-password/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="formData.status" placeholder="请选择">
                <el-option label="启用" :value="1"/>
                <el-option label="停用" :value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="saveEmployee">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'


const employees = ref([])
const total = ref(0)
const queryParams = ref({
  page: 1,
  size: 10,
  name: '',
  phone: '',
  role: '',
  status: ''
})

const roleMap = {
  0: '管理员',
  1: '仓库主管',
  2: '普通员工'
}

const statusMap = {
  0: '停用',
  1: '启用'
}

const dialogVisible = ref(false)
const isEdit = ref(false)
const formData = ref({
  id: null,
  username: '',
  password: '',
  name: '',
  phone: '',
  sex: '',
  id_number: '',
  role: '',
  status: 1
})

const formRef = ref(null)

const getList = async () => {
  const res = await request.get('/employee/', {params: queryParams.value})
  employees.value = res.records
  total.value = res.total
}

const addEmployee = () => {
  isEdit.value = false
  formData.value = {
    username: '',
    password: '',
    name: '',
    phone: '',
    sex: '',
    id_number: '',
    role: '',
    status: 1
  }
  dialogVisible.value = true
}

const editEmployee = (row) => {
  isEdit.value = true
  formData.value = {...row}
  dialogVisible.value = true
}

const deleteEmployee = async (id) => {
    try {
      const response = await request.delete(`/employee/${id}/`)
      console.log('Delete response:', response)
      ElMessage.success('删除成功')
      getList()
    } catch (error) {
      console.error('Delete error:', error)
      ElMessage.error('删除失败')
      // ElMessage.success('删除成功')
      getList()
    }
  }

const saveEmployee = async () => {
  try {
    if (isEdit.value) {
      // 编辑
      const res = await request.put(`/employee/${formData.value.id}/`, formData.value)
      ElMessage.success('更新成功')
    } else {
      // 新增
      const res = await request.post('/employee/', formData.value)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    getList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
